﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace ChamCong.Admin
{
    public partial class QuanLyBangCap : BasePage
    {
        BangCapBLL BCBLL = new BangCapBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            btnUpdate.Visible = false;
        }

        protected void btnThemBC_Click(object sender, EventArgs e)
        {
            if (txtMaBC.Text != "" && txtTenBC.Text != "")
            {
                BANGCAP BC = new BANGCAP();
                BC.MaBangCap = int.Parse(txtMaBC.Text);
                BC.TenBangCap = txtTenBC.Text;
                BC.MoTa = txtMoTa.Text;

                if (BCBLL.KiemTraTonTai(BC.MaBangCap, BC.TenBangCap) != null)
                {
                    sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", BCBLL.KiemTraTonTai(BC.MaBangCap, BC.TenBangCap) + " đã tồn tại!");
                }
                else
                {
                    BCBLL.ThemBC(BC);
                    dgvDSBangCap.DataBind();
                    Reset();
                    sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Thêm thành công!");
                } 
            }
            else
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Vui lòng nhập đầy đủ thông tin!");
            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //================================
            BANGCAP bc = new BANGCAP();
            bc.MaBangCap = int.Parse(txtMaBC.Text);
            bc.TenBangCap = txtTenBC.Text;
            bc.MoTa = txtMoTa.Text;

            if (BCBLL.KiemTraTenBC(bc.MaBangCap, bc.TenBangCap) == false)
            {
                BCBLL.SuaBC(bc);
                dgvDSBangCap.DataBind();
                Reset();
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Cập nhật thành công!");
            }
            else
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Tên Bằng Cấp đã được sử dụng!");            
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Reset();
        }

        //protected void libnXoa_Click(object sender, EventArgs e)
        //{
        //    int mabc = Convert.ToInt32(((LinkButton)sender).CommandArgument);
        //    try
        //    {
        //        BCBLL.XoaBC(mabc);
        //        dgvDSBangCap.DataBind();
        //        Reset();
        //    }
        //    catch (Exception ex)
        //    {
        //        lbthongbao2.Text = "Không thể xóa!";
        //    }
        //}

        //protected void imgbtXoa_Click(object sender, ImageClickEventArgs e)
        //{
        //    //lbthongbao.Text = "hello!";
        //    //int mabc = Convert.ToInt32(((LinkButton)sender).CommandArgument);
        //    //lbthongbao.Text = mabc.ToString();
        //    //lbthongbao.Text = "hi!";
        //    //try
        //    //{
        //    //    BCBLL.XoaBC(mabc);
        //    //    dgvDSBangCap.DataBind();
        //    //    Reset();
        //    //}
        //    //catch (Exception ex)
        //    //{
        //    //    lbthongbao2.Text = "Không thể xóa!";
        //    //}
        //    //lbthongbao.Text = "hello!";
        //}

        //protected void libnSua_Click(object sender, EventArgs e)
        //{
        //    int mabc = Convert.ToInt32(((LinkButton)sender).CommandArgument);
        //    if (IsPostBack)
        //    {
        //        BANGCAP BC = BCBLL.LayBangCap(mabc);
        //        txtMaBC.Text = BC.MaBangCap.ToString();
        //        txtMaBC.Enabled = false;
        //        txtTenBC.Text = BC.TenBangCap;
        //        txtMoTa.Text = BC.MoTa;

        //        ShowEditButton();
        //    }
        //}

        protected void libnChiTiet_Click(object sender, EventArgs e)
        {
            int mabc = Convert.ToInt32(((LinkButton)sender).CommandArgument);
            if (IsPostBack)
            {
                BANGCAP BC = BCBLL.LayBangCap(mabc);

                txtMaBC.Text = BC.MaBangCap.ToString();
                txtMaBC.Enabled = false;

                txtTenBC.Text = BC.TenBangCap;
                txtTenBC.Enabled = false;

                txtMoTa.Text = BC.MoTa;
                txtMoTa.Enabled = false;

                btnThemBC.Visible = false;
            }
        }

        protected void Reset()
        {
            txtMaBC.Text = null;
            txtMaBC.Enabled = true;
            txtMoTa.Text = null;
            txtMoTa.Enabled = true;
            txtTenBC.Text = null;
            txtTenBC.Enabled = true;

            btnThemBC.Visible = true;
            btnUpdate.Visible = false;
        }

        protected void ShowEditButton()
        {
            btnThemBC.Visible = false;
            btnUpdate.Visible = true;
        }

        protected void dgvDSBangCap_RowDataBond(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtXoa = (ImageButton)e.Row.FindControl("imgbtXoa");
                ImageButton imgbtSua = (ImageButton)e.Row.FindControl("imgbtSua");
                ImageButton imgbtXem = (ImageButton)e.Row.FindControl("imgbtXem");

                if (imgbtXoa != null)
                    imgbtXoa.Attributes["MaBangCap"] = e.Row.Cells[0].Text;
                if (imgbtSua != null)
                    imgbtSua.Attributes["MaBangCap"] = e.Row.Cells[0].Text;
                if (imgbtXem != null)
                    imgbtXem.Attributes["MaBangCap"] = e.Row.Cells[0].Text;
            }
        }

        protected void imgbtXoa_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int mabc = int.Parse(btn.Attributes["MaBangCap"]);

            try
            {
                BCBLL.XoaBC(mabc);
                dgvDSBangCap.DataBind();
                Reset();
            }
            catch
            {
                sbcMsgBox2.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Không thể xóa!");
            }
        }

        protected void imgbtSua_Click(object sender, ImageClickEventArgs e)
        {
            Reset();
            ImageButton btn = (ImageButton)sender;
            int mabc = int.Parse(btn.Attributes["MaBangCap"]);

            if (IsPostBack)
            {
                BANGCAP BC = BCBLL.LayBangCap(mabc);
                txtMaBC.Text = BC.MaBangCap.ToString();
                txtMaBC.Enabled = false;
                txtTenBC.Text = BC.TenBangCap;
                txtMoTa.Text = BC.MoTa;

                ShowEditButton();
            }
        }

        protected void imgbtXem_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int mabc = int.Parse(btn.Attributes["MaBangCap"]);

            if (IsPostBack)
            {
                BANGCAP BC = BCBLL.LayBangCap(mabc);

                txtMaBC.Text = BC.MaBangCap.ToString();
                txtMaBC.Enabled = false;

                txtTenBC.Text = BC.TenBangCap;
                txtTenBC.Enabled = false;

                txtMoTa.Text = BC.MoTa;
                txtMoTa.Enabled = false;

                btnThemBC.Visible = false;
            }
        }
    }
}